//
//  discoverTitleController.m
//  Focus
//
//  Created by xwc on 14-10-30.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "discoverTitleController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "discoverTitleView.h"
#import "NilViewController.h"
#import "MBProgressHUD.h"
@interface discoverTitleController ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIButton *button_back;

@property (nonatomic, retain)NSMutableDictionary *dicData;
@property (nonatomic, retain)NSMutableArray *articlesArr;
@property (nonatomic, retain)NSMutableArray *pagesArr;

@property (nonatomic, retain)discoverTitleModel *discoverModel;
@property (nonatomic, assign)NSInteger dataCount;
@property (nonatomic, retain) UIScrollView * scroll;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, assign) CGPoint point;
@property (nonatomic, assign) CGPoint point1;


@end
static int x = 1;
static int ye = 1;
@implementation discoverTitleController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.dicData = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark 页面布局
    [self setupSubViews];
    
#pragma mark 网络申请
    [self connect];
}

- (void)connect
{
#pragma mark 风火轮
    MBProgressHUD *hub = [[MBProgressHUD alloc]initWithView:self.view];
    hub.labelText = @"加载";
    [hub setMode:MBProgressHUDModeIndeterminate];
    [hub show:YES];
    [self.view addSubview:hub];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    [manager GET:self.api_url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.dicData = responseObject;
        self.articlesArr = [[self.dicData objectForKey:@"data"] objectForKey:@"articles"];
        self.pagesArr = [[[self.dicData objectForKey:@"data"] objectForKey:@"ipadconfig"] objectForKey:@"pages"];
        [hub removeFromSuperview];
        [self loadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //        NSLog(@"Error: %@", error);
        //        NSLog(@"%@",operation.responseString);
        [hub removeFromSuperview];
    }];
}

- (void)setupSubViews
{
#pragma mark 设置scrollView
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 70)];
    self.scrollView.contentSize = CGSizeMake(2000, 0);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
#pragma mark 设置返回按钮
    self.button_back = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button_back.frame = CGRectMake(0, self.view.frame.size.height - 50, 50, 50);
    [self.button_back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.button_back addTarget:self action:@selector(button_backClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button_back];
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(80, self.view.frame.size.height / 80 * 76, 130, 20)];
    self.scroll.backgroundColor = [UIColor whiteColor];
    self.scroll.contentSize = CGSizeMake(130, 0);
    self.scroll.bounces = NO;
    
    [self.view addSubview:self.scroll];
    [self.scroll release];
#pragma mark 滚动线
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(2, 9, 130, 1)];
    view.backgroundColor = [UIColor grayColor];
    [self.scroll addSubview:view];
    [view release];
#pragma mark 按钮
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(2,1,16, 16)];
    self.label.backgroundColor = [UIColor grayColor];
    self.label.text =[NSString stringWithFormat:@"%d", x];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.textColor = [UIColor whiteColor];
    
    self.label.userInteractionEnabled = YES;
    [self.scroll addSubview:self.label];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    pan.minimumNumberOfTouches = 1;
    pan.maximumNumberOfTouches = 1;
    [self.label addGestureRecognizer:pan];
    [pan release];
}

#pragma mark 手势方法
- (void)pan:(UIPanGestureRecognizer *)ppp
{
    NSLog(@"拽一拽");
    NSLog(@"%f", ppp.view.center.x);
    
    [UIView animateWithDuration:0.2 animations:^{
        
        if (ppp.state != UIGestureRecognizerStateEnded && ppp.state != UIGestureRecognizerStateFailed) {
            CGPoint location = [ppp locationInView:ppp.view.superview];
            
            location.y = 9;
            self.point1 = location;
            if (location.x < 9) {
                
                self.label.text = @"1";
                self.scrollView.contentOffset = CGPointMake(0, 0);
                location.x = 10;
                ppp.view.center = location;
                
            }else if (location.x > 40 &&location.x < 60) {
                
                self.label.text = @"2";
                
                location.x = 41;
                self.scrollView.contentOffset = CGPointMake(320 , 0);
                ppp.view.center = location;
            } else if (location.x > 60 &&location.x < 80) {
                
                location.x = 61;
                self.scrollView.contentOffset = CGPointMake(320 * 2, 0);
                self.label.text = @"3";
                ppp.view.center = location;
            } else if (location.x > 80 &&location.x < 100) {
                location.x = 81;
                self.scrollView.contentOffset = CGPointMake(320 * 3, 0);
                self.label.text = @"4";
                ppp.view.center = location;
            } else if (location.x > 100 &&location.x < 120) {
                location.x = 101;
                self.scrollView.contentOffset = CGPointMake(320 * 4, 0);
                self.label.text = @"5";
                ppp.view.center = location;
            } else if (location.x > 120 &&location.x < 140) {
                location.x = 121;
                self.scrollView.contentOffset = CGPointMake(320 * 5, 0);
                self.label.text = @"6";
                ppp.view.center = location;
            } else if (location.x > 120){
                
                
                location.x = 121;
                ppp.view.center = location;
            }
        }
    }];
    
}

#pragma mark 拉动结束的方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.scrollView.contentOffset.x >= 0 && self.point.x >= 0 ) {
        
        if ( self.scrollView.contentOffset.x  - self.point.x > 0 ) {
            x++;
            ye++;
            
            self.label.center =CGPointMake(self.label.center.x + 22, 9);
            //            NSLog(@"self.label.center.x = %f", self.label.center.x);
            
            [self.label setText:[NSString stringWithFormat:@"%d", x]];
            
            if (self.point.x > (self.pagesArr.count - 2) * 320) {
                
                self.label.center = CGPointMake(120, 9);
                [self.label setText:[NSString stringWithFormat:@"%d", self.pagesArr.count]];
                ye = 6;
            }
            
        } else {
            
            //
            x--;
            ye--;
            self.label.center =CGPointMake(self.label.center.x - 22, 9);
            [self.label setText:[NSString stringWithFormat:@"%d", ye]];
            if (self.point.x == 0 ) {
                self.label.center = CGPointMake(2, 9);
                self.label.text = @"1";
                
            }
            x = 1;
            
        }
    }
    //    NSLog(@"  ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ self.collerction.contentOffset.x = %f,  self.point.x = %f ", self.collerction.contentOffset.x , self.point.x);
    //    NSLog(@"self.collerction.contentOffset.x  = %f, 320 * self.finalArr.count = %d", self.collerction.contentOffset.x * self.finalArr.count, 320 * self.finalArr.count);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.point  = self.scrollView.contentOffset;
}



- (void)viewDidDisappear:(BOOL)animated
{
    [self.view reloadInputViews];
    x = 1;
    ye = 1;
}




#pragma mark 根据偏移量来计算当前的页数
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    //    NSLog(@"偏移量改变");
}

- (void)loadData
{
    if ([[self.dicData objectForKey:@"msg"] isEqualToString:@"频道已下架"]) {
        NilViewController *nilVC = [[NilViewController alloc]init];
        [self presentViewController:nilVC animated:YES completion:^{
        }];
    }
    int count = [[[[self.dicData objectForKey:@"data"] objectForKey:@"ipadconfig"] objectForKey:@"pages"] count];
    //设置scrollView偏移量
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * count, 0);
    
    //设置多少个discoverView个数并且赋值
    for (int i = 0; i < count; i++) {
        discoverTitleView *discoverView = [[discoverTitleView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height - 70)];
        [self.scrollView addSubview:discoverView];
        [discoverView release];
        
        discoverTitleModel *discover = [[discoverTitleModel alloc] initWithDictionary:self.articlesArr pagesArr:self.pagesArr pagesNumber:i];
        discoverView.viewController = self;
        discoverView.discoverModel = discover;
        discoverView.viewController = self;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)button_backClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
